package org.openlab.model;

import java.io.Reader;
import java.io.StringReader;
import java.util.Scanner;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import com.google.common.collect.Sets;

@Data
@EqualsAndHashCode(callSuper=false)
@Entity
@Table(name = "ExchangeAttribute")
@NoArgsConstructor
public class ExchangeAttribute extends BaseEntity  {
	@Transient
	private static final long serialVersionUID = -6660037812427575980L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	
	@Column(nullable = false)
	private String rdm;
	@Column(nullable = false)
	private boolean primaryKey;
	@Column(nullable = false)
	private boolean transactable;
	
	@Column(nullable = false)
	private String senderType;
	@Column(nullable = false)
	private String sender;
	
	@Column(nullable = true)
	private String receiver;
	@Column(nullable = false)
	private boolean required;
	
	@Column(nullable = true)
	private String filter;
	@Column(nullable = true)
	private String comment;
	
	public static ExchangeAttribute newAttribute() {
		ExchangeAttribute attr = new ExchangeAttribute();
		attr.setPrimaryKey(false);
		attr.setTransactable(true);
		attr.setRequired(true);
		return attr;
	}
	
	public static Set<ExchangeAttribute> extract(HttpServletRequest request) {
		Set<ExchangeAttribute> attributes = Sets.newHashSet();
		String[] rdm = request.getParameterValues("rdms");
		if (rdm != null && rdm.length > 0) {
			String[] pk = request.getParameter("input_pk").split("&");
			String[] trans = request.getParameter("input_trans").split("&");
			String[] senderType = request.getParameter("input_senderType").split("&");
			String[] sender = request.getParameterValues("senders");
			String[] receiver = request.getParameterValues("receivers");
			String[] required = request.getParameter("input_required").split("&");
			String[] filter = request.getParameterValues("filters");
			String[] comment = request.getParameterValues("comments");
			for (int i=0; i< rdm.length; i++) {
				attributes.add(new ExchangeAttribute(rdm[i], "true".equals(pk[i]), "true".equals(trans[i]), senderType[i], sender[i], receiver[i], "true".equals(required[i]), filter[i], comment[i]));
			}
		}
		return attributes;
	}
	
	public static Set<ExchangeAttribute> importing(HttpServletRequest request) {
		Set<ExchangeAttribute> attributes = Sets.newHashSet();
		String rdmStr = request.getParameter("rdm");
		String fromStr = request.getParameter("send");
		String toStr = request.getParameter("recv");
		String reqStr = request.getParameter("req");
		Scanner rdmer = new Scanner(rdmStr);
		Scanner fromer = new Scanner(fromStr);
		Scanner toer = new Scanner(toStr);
		Scanner reqer = new Scanner(reqStr);
		while (rdmer.hasNextLine()) {
			String rdm = rdmer.nextLine();
			String from = fromer.nextLine();
			String to = toer.nextLine();
			String req = reqer.nextLine();
			ExchangeAttribute att = newAttribute();
			att.setRdm(rdm);
			att.setSender(from);
			if (StringUtils.isBlank(from)) {
				att.setSenderType("DEFAULT");
			}
			att.setReceiver(to);
			att.setRequired("Required".equals(req));
			attributes.add(att);
		}
		return attributes;
	}
	
	public ExchangeAttribute(String rdm, boolean primaryKey,
			boolean transactable, String senderType, String sender,
			String receiver, boolean required, String filter, String comment) {
		super();
		this.rdm = rdm;
		this.primaryKey = primaryKey;
		this.transactable = transactable;
		this.senderType = senderType;
		this.sender = sender;
		this.receiver = receiver;
		this.required = required;
		this.filter = filter;
		this.comment = comment;
	}
	
	public static void main(String[] args) {
		String a = "a&&b&";
		for(String bb:a.split("&")){
		System.out.println(bb);
		}
	}
}
